package com.hzsd.zengbanyi.biaodan_text.bean;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.weight.Bimp;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PictureAdapter extends BaseAdapter {
	private TextCallback textcallback = null;
	private LayoutInflater inflater;
	private List<Picture> pictures;
	private Context context;
	private String[] titles;
	private String[] images;
	private Handler mHandler;
	public int selectTotal = 0;
	private String taskSenceId;
	private WorkDAO workDao;
	public Map<String, String> map = new HashMap<String, String>();

	public static interface TextCallback {
		public void onListen(int count);
	}

	public void setTextCallback(TextCallback listener) {
		textcallback = listener;
	}
	public PictureAdapter(String[] titles, String[] images, Context context,Handler mHandler,String taskSenceId) {
		this.context=context;
		this.titles=titles;
		this.images=images;
		this.taskSenceId=taskSenceId;
		this.mHandler = mHandler;
		pictures = new ArrayList<Picture>();
		inflater = LayoutInflater.from(context);
		for (int i = 0; i < images.length; i++) {
			Picture picture = new Picture(titles[i], images[i]);
			pictures.add(picture);
		}
		workDao=new WorkDAO(context);
	}
	

	@Override
	public int getCount() {
		if (null != pictures) {
			return pictures.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return pictures.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	/*class Holder {
		private ImageView iv;
		private ImageView selected;
		private TextView text;
	}*/

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		/*final Holder holder;

		if (convertView == null) {
			holder = new Holder();
			convertView = View.inflate(context, R.layout.item_image_grid, null);
			holder.iv = (ImageView) convertView.findViewById(R.id.image);
			holder.selected = (ImageView) convertView
					.findViewById(R.id.isselected);
			holder.text = (TextView) convertView
					.findViewById(R.id.item_image_grid_text);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final Picture item = pictures.get(position);

		holder.iv.setTag(item.getImageId());
		cache.displayBmp(holder.iv, item.thumbnailPath, item.imagePath,
				callback);
		if (item.isSelected) {
			holder.selected.setImageResource(R.drawable.icon_data_select);  
			holder.text.setBackgroundResource(R.drawable.bgd_relatly_line);
		} else {
			holder.selected.setImageResource(-1);
			holder.text.setBackgroundColor(0x00000000);
		}
		holder.iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String path = dataList.get(position).imagePath;
				if ((Bimp.bmp.size() + selectTotal) < 9) {
					if (workDao.IsSamePicSceneId(dataList.get(position).imagePath,taskSenceId)) {
						Message message = Message.obtain(mHandler, 1);
						message.sendToTarget();
					}else{
					item.isSelected = !item.isSelected;
					if (item.isSelected) {
						holder.selected
								.setImageResource(R.drawable.icon_data_select);
						holder.text.setBackgroundResource(R.drawable.bgd_relatly_line);
						selectTotal++;
						if (textcallback != null)
							textcallback.onListen(selectTotal);
						map.put(path, path);

					} else if (!item.isSelected) {
						holder.selected.setImageResource(-1);
						holder.text.setBackgroundColor(0x00000000);
						selectTotal--;
						if (textcallback != null)
							textcallback.onListen(selectTotal);
						map.remove(path);
					}	
					
					}//判断是否有相同的path
				} else if ((Bimp.bmp.size() + selectTotal) >= 9) {
					if (item.isSelected == true) {
						item.isSelected = !item.isSelected;
						holder.selected.setImageResource(-1);
						selectTotal--;
						map.remove(path);

					} else {
						Message message = Message.obtain(mHandler, 0);
						message.sendToTarget();
					}
				}
			}

		});

		return convertView;*/
	
		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_image_grid, null);
			viewHolder = new ViewHolder();
			//viewHolder.title = (TextView) convertView.findViewById(R.id.title);
			viewHolder.iv = (ImageView) convertView.findViewById(R.id.image);
			viewHolder.selected = (ImageView) convertView
					.findViewById(R.id.isselected);
			viewHolder.text = (TextView) convertView
					.findViewById(R.id.item_image_grid_text);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		//viewHolder.title.setText(pictures.get(position).getTitle());
		FinalBitmap finalBitmap = FinalBitmap.create(context);
		finalBitmap.display(viewHolder.iv, pictures.get(position).getImageId());
//		finalBitmap.display(viewHolder.iv, null, BitmapFactory.decodeFile(pictures.get(
//				position).getImageId()));
//		viewHolder.iv.setImageBitmap(BitmapFactory.decodeFile(pictures.get(
//				position).getImageId()));
		final Picture picture=pictures.get(position);
		
		if(picture.isSelected){
			viewHolder.selected.setImageResource(R.drawable.icon_data_select);
			viewHolder.text.setBackgroundResource(R.drawable.bgd_relatly_line);
		}else {
			viewHolder.selected.setImageResource(-1);
			viewHolder.text.setBackgroundColor(0x00000000);
		}
		viewHolder.iv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String path=pictures.get(position).imageId;
				//Toast.makeText(context, position+"", Toast.LENGTH_SHORT).show();
				if((Bimp.bmp.size()+selectTotal)<9){
					if(workDao.IsSamePicSceneId(pictures.get(position).imageId,taskSenceId)){//获取图片path是否有相同
						Message message = Message.obtain(mHandler, 1);
						message.sendToTarget();
					}else{
						picture.isSelected=!picture.isSelected;
						if(picture.isSelected){
							viewHolder.selected.setImageResource(R.drawable.icon_data_select);
							viewHolder.text.setBackgroundResource(R.drawable.bgd_relatly_line);
							selectTotal++;
							if (textcallback != null)
								textcallback.onListen(selectTotal);
							map.put(path, path);
							
						}else if(!picture.isSelected){
							viewHolder.selected.setImageResource(-1);
							viewHolder.text.setBackgroundColor(0x00000000);
							selectTotal--;
							if (textcallback != null)
								textcallback.onListen(selectTotal);
							map.remove(path);
							
						}
					}
					
				}else if((Bimp.bmp.size() + selectTotal) >= 9){
					if (picture.isSelected == true) {
						picture.isSelected = !picture.isSelected;
						viewHolder.selected.setImageResource(-1);
						selectTotal--;
						map.remove(path);

					} else {
						Message message = Message.obtain(mHandler, 0);
						message.sendToTarget();
					}
				}
				
			}
		});
		return convertView;
		
	}
	
}
class ViewHolder { 
	public ImageView iv;
	public ImageView selected;
	public TextView text; 
	}
